<?php

class Module_Mediate_CaseRepayLog extends Module_BaseModule
{
    use Lib_BaseTraitsModule;
    private static $obj = null;
    private $model = null;
    // 还款
    public static $repayStatusArr = array(
        1 => '确认还款并结案',
        2 => '确认还款',
        3 => '信息有误关闭',
    );

    /**
     * 封闭构造.
     */
    public function __construct()
    {
        $this->model = new Model_Mediate_CaseRepayLog();
    }


    /**
     * 单例获取
     * 保证一条进程只产生一个Module对象
     *
     * @return Module_Mediate_CaseRepayLog
     */
    public static function getInstance()
    {
        if (empty(self::$obj)) {
            self::$obj = new self();
        }

        return self::$obj;
    }

    public function getDataBySearch($fields = array(), $search = array(), $cache = false){
        return $this->model->getDataBySearch($fields, $search, $cache);
    }
    /**
     * @desc 分页查询 获取上一页 下一页 id
     * @param int $type 1上一页  2 下一页
     * @param int $id
     *
     * @return mixed
     */
    public function getPageId($type = 1, $id = 0, $adm_id)
    {
        return $this->model->getPageId($type, $id, $adm_id);
    }


    /**
     * @desc 分页查询 获取上一页 下一页 id
     * @param int $type 1上一页  2 下一页
     * @param int $id
     *
     * @return mixed
     */
    public function getPageIdNew($type = 1, $id = 0, $adm,$auditrepay = 0)
    {
        return $this->model->getPageIdNew($type, $id, $adm,$auditrepay);
    }

    /**
     * @desc接口推送还款资料
     * @param array $data
     * @param $uid
     * @param $name
     * @return array
     */
    public function apiAddData(array $data)
    {
        return $this->model->apiAddData($data);
    }
    /**
     * @desc 原告上传资料
     * @param array $data
     * @param $uid
     * @param $name
     * @return array
     */
    public function saveData(array $data, $uid, $name)
    {
        return $this->model->saveData($data, $uid, $name);
    }


    /**
     * @desc 原告上传资料 定时任务
     * @param $case_id
     * @param $id
     * @return bool
     */
    public function timingSaveData($case_id, $id)
    {
        if (!$case_id || !$id) {
            return false;
        }
        return $this->model->timingSaveData($case_id, $id);
    }

    /**
     * @desc 确认还款
     *
     * @param $id
     * @param $status
     *
     * @return bool
     */
    public function updateStatus($id = 0, $status = 0, $userinfo = [])
    {
        if (!$id || !$status) {
            return false;
        }

        return $this->model->updateStatus($id, $status, $userinfo);
    }

    /**
     * @desc 信息有误关闭
     * @param $id
     * @param $status
     * @return bool
     */
    public function closeRepayLog($id = 0, $status = 0)
    {
        if (!$id) {
            return false;
        }
        return $this->model->closeRepayLog($id, $status);
    }


    /**
     *  分页查询.
     */
    public function getAllList($search = [], $count = false, $cache = false)
    {
        if (empty($search)) {
            return false;
        }

        return $this->model->getAllList($search, $count, $cache);
    }

    /**
     *  分页查询(经办人).
     */
    public function getAllByAmdId($adm_id = 0, $where = [] ,$count = false, $page = 1, $limit = 10, $cache = false)
    {
        if (empty($adm_id)) {
            return false;
        }
        $start = ($page - 1) * $limit;
        return $this->model->getAllByAmdId($adm_id, $where ,$count, $start, $limit, $cache);
    }
    public function getlistByAmd($user_type, $firm_id ,$type, $start, $limit = 10, $count =false,$cache = false)
    {

        return $this->model->getlistByAmd($user_type, $firm_id ,$type, $start, $limit , $count ,$cache);
    }
    /**
     * @desc 判断当前是否为最后一条
     * @param int $case_id
     * @param int $repayLogid
     * @return bool
     */
    public function isLast($case_id = 0, $repayLogid = 0)
    {
        $res = $this->model->getOneByCaseId($case_id);
        if (!empty($res) && count($res) == 1 && $res[0]['id'] == $repayLogid) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param int $case_id
     * @return bool
     */
    public function getOneByCaseId($case_id = 0){
        if(!$case_id){
            return false;
        }
        return $this->model->getOneByCaseId($case_id);
    }

    /**
     * 根据被告人上传信息更新数据表
     *
     * @param string $filePath
     * @param array $caseData
     * @return boolean
     */
    public function updateByDefendantUploadData(string $filePath = '', array $caseData = []): bool
    {
        return $this->model->updateByDefendantUploadData($filePath, $caseData);
    }

    //获取案件还款总额
    public function getCaseRepayAmount($case_id){
       $repay_data = $this->model->getCaseRepayAmount($case_id);
       return $repay_data['total_amount'] ? $repay_data['total_amount'] : 0;

    }
}
